<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">用户登录</h1>
<!--        登录表单-->
        <el-form :model="loginForm" label-width="100px"
                 :rules="rules" ref="loginForm">
          <el-form-item label="账号" prop="no">
            <el-input style="width: 200px" type="text" v-model="loginForm.no"
                      autocomplete="off" size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input style="width: 200px" type="password" v-model="loginForm.password"
                      show-password autocomplete="off" size="small" @keyup.enter="confirm"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm" :disabled="confirm_disabled">确 定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>


<script>
export default {
  name: "Login",
  data(){
    return{
      confirm_disabled:false,//按钮是否可用
      loginForm:{
        no:'',
        password:''
      },
      rules:{
        no: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输密码', trigger: 'blur' }
        ],
      }
    }
  },
  methods:{

    confirm(){ //登录
      this.confirm_disabled=true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) { //valid成功为true，失败为false
          // 步骤1：调用 /login 获取 JWT
          this.$axios.post(this.$httpUrl + '/login', this.loginForm).then(res => res.data).then(res => {
            if (res.code === 200 && res.data) {
              const token = res.data;
              // 存储 JWT
              sessionStorage.setItem('jwtToken', token);

              // 步骤2：调用受保护的 /user/me 获取用户与菜单（此请求将自动携带 Authorization）
              this.$axios.get(this.$httpUrl + '/user/me').then(r => r.data).then(r => {
                if (r.code === 200) {
                  //存储用户信息
                  sessionStorage.setItem("CurUser", JSON.stringify(r.data.user))
                  //存储菜单并动态挂载路由
                  this.$store.commit("setMenu", r.data.menu)
                  //跳转到主页Index
                  this.$router.replace('/Index');
                } else {
                  this.confirm_disabled=false;
                  alert('获取用户信息失败！');
                }
              }).catch(() => {
                this.confirm_disabled=false;
                alert('获取用户信息失败！');
              });
            } else {
              this.confirm_disabled=false;
              alert('校验失败，用户名或密码错误！');
            }
          }).catch(() => {
            this.confirm_disabled=false;
            alert('登录请求失败！');
          });
        } else {
          this.confirm_disabled=false;
          console.log('校验失败');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url('~@/assets/login-bg.jpg') no-repeat center center fixed;
  background-size: cover;
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.login-title {
  margin: 20px 0;
  text-align: center;
  color: #555;
}
.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
.el-input__inner {
  border-color: #ddd;
}
.el-button--primary {
  background-color: #666;
  border-color: #666;
}
.el-button--primary:hover {
  background-color: #555;
  border-color: #555;
}
</style>